package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class AddActivity extends AppCompatActivity {

    private EditText et_name,et_age;

    private int[] imgIds = {R.drawable.dog1,R.drawable.dog2,R.drawable.dog3,
            R.drawable.dog4,R.drawable.dog5};

    private Button saveBtn;

    private RadioGroup radioGroup;
    private int imgIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        radioGroup = findViewById(R.id.rg_dogs);

        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);

        saveBtn = findViewById(R.id.btn_save);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_dog1:
                        imgIndex = 0;
                        break;
                    case R.id.rb_dog2:
                        imgIndex = 1;
                        break;
                    case R.id.rb_dog3:
                        imgIndex = 2;
                        break;
                    case R.id.rb_dog4:
                        imgIndex = 3;
                        break;
                    case R.id.rb_dog5:
                        imgIndex = 4;
                        break;
                }
            }
        });

        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddActivity.this,MainActivity.class);

                intent.putExtra("imgId",imgIds[imgIndex]);
                intent.putExtra("name",et_name.getText().toString());
                intent.putExtra("age",Integer.parseInt(et_age.getText().toString()));

                setResult(101,intent);
                finish();
            }
        });

    }
}
